/*
@author: Shizuku
@file: 2022-11-03-practice.go
@time: 2022/11/3 17:18
*/
package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strings"
)

func main() {
	//exer1()
	//exer2()
	exer3()
}

func exer1() {
	num := 0
	for i := 0; i < 8; i++{
		num = i*2 + 1
		star := strings.Repeat("*",num)
		fmt.Println(star)
	}
}

func exer2() {
	for i := 1; i < 10; i++ {
		for j := 1; j <= i; j++ {
			fmt.Print(j, "x", i, "=", i*j, " ")
		}
		fmt.Println()
	}
}

func exer3() {
	fmt.Println("请输入一段字符：")
	for {
		var choice string
		r := bufio.NewReader(os.Stdin)
		str1, _:=r.ReadString('\n')
		pat := "[^A-Za-z0-9]"
		re, _ := regexp.Compile(pat)
		str := re.ReplaceAllString(str1, "")
		newstr := stringReverse(str)
		if str == newstr {
			fmt.Println("是回文")
		} else {
			fmt.Println("不是回文")
		}
		fmt.Println("按q退出或按任意字符回车继续输入")
		fmt.Scan(&choice)
		if choice == "q" {
			break
		}
	}
}

func stringReverse(str string)  string{
	var bytes []byte = []byte(str)
	for i := 0; i < len(str)/2; i++ {
		tmp := bytes[len(str) - i - 1]
		bytes[len(str) - i - 1] = bytes[i]
		bytes[i] = tmp
	}
	str2 := string(bytes)
	return str2
}